<template>
  <div class="cx-picker-color">
    <!-- 颜色预览区域 -->
    <div v-if="showPreview" class="color-preview">
      <label v-if="label" class="color-label">{{ label }}</label>
      <el-popover
        placement="bottom"
        width="280"
        trigger="click"
        v-model="showPicker"
      >
        <div class="color-picker-popover">
          <el-color-picker
            v-model="internalValue"
            :show-alpha="false"
            :predefine="predefineColors"
            @change="handleChange"
          />
          
          <!-- 预设颜色 -->
          <div class="preset-colors">
            <div
              v-for="(color, index) in predefineColors"
              :key="index"
              class="preset-color-item"
              :class="{ active: internalValue === color }"
              :style="{ backgroundColor: color }"
              @click="selectPresetColor(color)"
            ></div>
          </div>
        </div>
        
        <template #reference>
<div  class="color-display">
          <div class="color-block" :style="{ backgroundColor: internalValue }"></div>
          <span class="color-text">{{ internalValue }}</span>
          <i class="el-icon-arrow-down arrow-icon"></i>
        </div>
</template>
      </el-popover>
    </div>
    
    <!-- 纯选择器模式 -->
    <el-color-picker
      v-else
      v-model="internalValue"
      :show-alpha="false"
      :predefine="predefineColors"
      @change="handleChange"
    />
  </div>
</template>

<script>
/**
 * 组件名称: PickerColor (Element UI 版本)
 * 功能说明: 颜色选择器组件
 * 研发时间: 2025/11/17
 * 研发人员: AI Assistant
 */

export default {
  name: 'PickerColor',
  
  props: {
    value: {
      type: String,
      default: '#ff0000'
    },
    label: {
      type: String,
      default: ''
    },
    defaultColor: {
      type: String,
      default: '#ff0000'
    },
    showPreview: {
      type: Boolean,
      default: true
    }
  },
  
  data() {
    return {
      showPicker: false,
      predefineColors: [
        '#ff0000',
        '#ff7f00',
        '#ffff00',
        '#00ff00',
        '#00ffff',
        '#0000ff',
        '#8b00ff',
        '#ff00ff',
        '#000000',
        '#808080',
        '#c0c0c0',
        '#ffffff',
        '#f44336',
        '#e91e63',
        '#9c27b0',
        '#673ab7',
        '#3f51b5',
        '#2196f3',
        '#03a9f4',
        '#00bcd4',
        '#009688',
        '#4caf50',
        '#8bc34a',
        '#cddc39',
        '#ffeb3b',
        '#ffc107',
        '#ff9800',
        '#ff5722',
        '#795548',
        '#9e9e9e',
        '#607d8b',
        '#333333'
      ]
    }
  },
  
  computed: {
    internalValue: {
      get() {
        return this.value || this.defaultColor
      },
      set(val) {
        this.$emit('input', val)
        this.$emit('update:value', val)
      }
    }
  },
  
  methods: {
    handleChange(color) {
      this.internalValue = color
      this.$emit('change', color)
      this.$emit('confirm', color)
    },
    
    selectPresetColor(color) {
      this.internalValue = color
      this.handleChange(color)
      this.showPicker = false
    }
  }
}
</script>

<style lang="scss" scoped>
.cx-picker-color {
  width: 100%;
}

.color-preview {
  background: #fff;
}

.color-label {
  font-size: 14px;
  color: #333;
  margin-bottom: 8px;
  display: block;
}

.color-display {
  display: flex;
  align-items: center;
  background-color: #f7f8fa;
  padding: 8px 12px;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid #dcdfe6;
  transition: all 0.3s;
  
  &:hover {
    border-color: #409eff;
  }
}

.color-block {
  width: 60px;
  height: 30px;
  border-radius: 4px;
  border: 1px solid #eee;
  margin-right: 12px;
}

.color-text {
  font-size: 14px;
  color: #666;
  flex: 1;
}

.color-right {
  display: flex;
  align-items: center;
}

.arrow-icon {
  font-size: 18px;
  margin-left: 8px;
  color: #999;
}

.color-picker-popover {
  padding: 10px;
}

.preset-colors {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 8px;
  margin-top: 10px;
}

.preset-color-item {
  width: 30px;
  height: 30px;
  border-radius: 4px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.3s;
  
  &:hover {
    transform: scale(1.1);
  }
  
  &.active {
    border-color: #409eff;
  }
}
</style>
